﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Blam_Unity.Classes.Engine.Halo_AA.gameAssetLevel.maps.info;

namespace Blam_Unity.Forms.Halo_AA.gameAssetLevel.maps.info
{
    public partial class frmMapInfo : Form
    {
        public string fileName;

        string[] mapNames = new string[11];
        string[] mapDesc = new string[11];

        string[] rallyPoints = new string[35];

        int previousMapIndex = -2;
        int previousRallyPointIndex = -2;

        public frmMapInfo()
        {
            InitializeComponent();
        }

        private void frmMapInfo_Load(object sender, EventArgs e)
        {
            MojosIO mio = new MojosIO(fileName, Endian.Big);

            mapNames = readMapInfoData.readUniLanMapName(mio);
            mapDesc = readMapInfoData.readuniLanMapDesc(mio);

            txtEngineMapName.Text = readMapInfoData.readInternalEngineMapName(mio);
            txtAssetMapName.Text = readMapInfoData.readPhysicalAssetMapName(mio);

            this.Text += fileName;

            if (readMapInfoData.readGetMapEngineType(mio) == "mp")
            {
                groupBox4.Enabled = false;
            }
            else if (readMapInfoData.readGetMapEngineType(mio) == "sp")
            {
                rallyPoints = readMapInfoData.readMissionRallyPoints(mio);

                if (rallyPoints[0] == "1") { cBRallyPointLan.Items.AddRange(new object[] { rallyPoints[2] }); }
                else if (rallyPoints[0] == "2") { cBRallyPointLan.Items.AddRange(new object[] { rallyPoints[2], rallyPoints[13] }); }
                else if (rallyPoints[0] == "3") { cBRallyPointLan.Items.AddRange(new object[] { rallyPoints[2], rallyPoints[13], rallyPoints[24] }); }
            }
            if (readMapInfoData.readGetMapEngineType(mio) == "ff")
            {
                rallyPoints = readMapInfoData.readMissionRallyPoints(mio);
                cBRallyPointLan.Items.AddRange(new object[] { rallyPoints[2] });
            }

            if (rallyPoints != null)
            {
                cBRallyPointLan.SelectedIndex++;
            }

            cBMpInfoLan.SelectedIndex++;
        }

        private void cBMpInfoLan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (previousMapIndex == -2) { }
            else
            {
                mapNames[previousMapIndex] = txtSelectedMapName.Text;
                mapDesc[previousMapIndex] = txtSelectedMapDesc.Text;
            }

            txtSelectedMapName.Text = mapNames[cBMpInfoLan.SelectedIndex];
            txtSelectedMapDesc.Text = mapDesc[cBMpInfoLan.SelectedIndex];

            previousMapIndex = cBMpInfoLan.SelectedIndex;
        }

        private void cBRallyPointLan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (previousRallyPointIndex == -2) { }
            else
            {
                if (previousRallyPointIndex == 0)
                {
                    rallyPoints[1] = txtRallyPointEngineCall.Text;
                    rallyPoints[2] = txtRallyPointEnglish.Text;
                    rallyPoints[3] = txtRallyPointJapanese.Text;
                    rallyPoints[4] = txtRallyPointGerman.Text;
                    rallyPoints[5] = txtRallyPointFrench.Text;
                    rallyPoints[6] = txtRallyPointSpanish.Text;
                    rallyPoints[7] = txtRallyPointLatinAmericanSpanish.Text;
                    rallyPoints[8] = txtRallyPointitalian.Text;
                    rallyPoints[9] = txtRallyPointKorean.Text;
                    rallyPoints[10] = txtRallyPointChinese.Text;
                    rallyPoints[11] = txtRallyPointPortuguese.Text;
                }
                else if (previousRallyPointIndex == 1)
                {
                    rallyPoints[12] = txtRallyPointEngineCall.Text;
                    rallyPoints[13] = txtRallyPointEnglish.Text;
                    rallyPoints[14] = txtRallyPointJapanese.Text;
                    rallyPoints[15] = txtRallyPointGerman.Text;
                    rallyPoints[16] = txtRallyPointFrench.Text;
                    rallyPoints[17] = txtRallyPointSpanish.Text;
                    rallyPoints[18] = txtRallyPointLatinAmericanSpanish.Text;
                    rallyPoints[19] = txtRallyPointitalian.Text;
                    rallyPoints[20] = txtRallyPointKorean.Text;
                    rallyPoints[21] = txtRallyPointChinese.Text;
                    rallyPoints[22] = txtRallyPointPortuguese.Text;
                }
                else if (previousRallyPointIndex == 2)
                {
                    rallyPoints[23] = txtRallyPointEngineCall.Text;
                    rallyPoints[24] = txtRallyPointEnglish.Text;
                    rallyPoints[25] = txtRallyPointJapanese.Text;
                    rallyPoints[26] = txtRallyPointGerman.Text;
                    rallyPoints[27] = txtRallyPointFrench.Text;
                    rallyPoints[28] = txtRallyPointSpanish.Text;
                    rallyPoints[29] = txtRallyPointLatinAmericanSpanish.Text;
                    rallyPoints[30] = txtRallyPointitalian.Text;
                    rallyPoints[31] = txtRallyPointKorean.Text;
                    rallyPoints[32] = txtRallyPointChinese.Text;
                    rallyPoints[33] = txtRallyPointPortuguese.Text;
                }
            }

            if (cBRallyPointLan.SelectedIndex == 0)
            {
                txtRallyPointEngineCall.Text = rallyPoints[1];
                txtRallyPointEnglish.Text = rallyPoints[2];
                txtRallyPointJapanese.Text = rallyPoints[3];
                txtRallyPointGerman.Text = rallyPoints[4];
                txtRallyPointFrench.Text = rallyPoints[5];
                txtRallyPointSpanish.Text = rallyPoints[6];
                txtRallyPointLatinAmericanSpanish.Text = rallyPoints[7];
                txtRallyPointitalian.Text = rallyPoints[8];
                txtRallyPointKorean.Text = rallyPoints[9];
                txtRallyPointChinese.Text = rallyPoints[10];
                txtRallyPointPortuguese.Text = rallyPoints[11];
            }
            else if (cBRallyPointLan.SelectedIndex == 1)
            {
                txtRallyPointEngineCall.Text = rallyPoints[12];
                txtRallyPointEnglish.Text = rallyPoints[13];
                txtRallyPointJapanese.Text = rallyPoints[14];
                txtRallyPointGerman.Text = rallyPoints[15];
                txtRallyPointFrench.Text = rallyPoints[16];
                txtRallyPointSpanish.Text = rallyPoints[17];
                txtRallyPointLatinAmericanSpanish.Text = rallyPoints[18];
                txtRallyPointitalian.Text = rallyPoints[19];
                txtRallyPointKorean.Text = rallyPoints[20];
                txtRallyPointChinese.Text = rallyPoints[21];
                txtRallyPointPortuguese.Text = rallyPoints[22];
            }
            else if (cBRallyPointLan.SelectedIndex == 2)
            {
                txtRallyPointEngineCall.Text = rallyPoints[23];
                txtRallyPointEnglish.Text = rallyPoints[24];
                txtRallyPointJapanese.Text = rallyPoints[25];
                txtRallyPointGerman.Text = rallyPoints[26];
                txtRallyPointFrench.Text = rallyPoints[27];
                txtRallyPointSpanish.Text = rallyPoints[28];
                txtRallyPointLatinAmericanSpanish.Text = rallyPoints[29];
                txtRallyPointitalian.Text = rallyPoints[30];
                txtRallyPointKorean.Text = rallyPoints[31];
                txtRallyPointChinese.Text = rallyPoints[32];
                txtRallyPointPortuguese.Text = rallyPoints[33];
            }


            previousRallyPointIndex = cBRallyPointLan.SelectedIndex;
        }

        public void saveMapInfo()
        {
            MojosIO mio = new MojosIO(fileName, Endian.Big);

            saveMapInfoData.saveInternalEngineMapName(mio, txtEngineMapName.Text);
            saveMapInfoData.savePhysicalAssetMapName(mio, txtAssetMapName.Text);

            saveMapInfoData.saveUniLanMapDesc(mio, mapDesc);
            saveMapInfoData.saveUniLanMapName(mio, mapNames);

            int rallyPointCount = Convert.ToInt32(rallyPoints[0]);

            if (rallyPointCount <= 1)
                saveMapInfoData.saveUniRallyPoint1(mio, rallyPoints);
            if (rallyPointCount <= 2)
                saveMapInfoData.saveUniRallyPoint2(mio, rallyPoints);
            if (rallyPointCount == 3)
                saveMapInfoData.saveUniRallyPoint3(mio, rallyPoints);

            mio.Flush();
            mio.Close();
        }
    }
}